<template>
  <div>
    <van-cell title="客户快递信息" icon="logistics" size="large" />
    <van-form @submit="onSubmit" label-width="60px">
      <van-field
        v-model="form.name"
        name="name"
        label="姓名"
        placeholder="请输入您的姓名"
        :rules="[{ required: true, message: '请填写姓名' }]"
      />
      <van-field
        v-model="form.telephone"
        name="telephone"
        type="tel"
        label="手机"
        placeholder="请输入您的手机"
        :rules="[{ required: true, message: '请填写手机' }]"
      />
      <van-field
        v-model="form.address"
        name="address"
        label="地址"
        placeholder="请输入收货详细地址"
        :rules="[{ required: true, message: '请填写详细地址' }]"
      />
      <van-field
        v-model="form.remark"
        name="remark"
        label="备注"
        placeholder="请输入备注"
      />
      <div style="margin: 16px 32px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import {
  getWxExpressInfos,
  addWxExpressInfo,
  updateWxExpressInfo,
} from "@/api/sales";

export default {
  name: "Express",
  data() {
    return {
      title: this.$route.meta.title,
      form: {
        id: null,
        name: "",
        telephone: "",
        address: "",
        remark: "",
      },

      action: "new",
    };
  },
  mounted() {
    this.getExpressInfos();
  },
  methods: {
    getExpressInfos() {
      const params = {
        openid: localStorage.getItem("openid"),
      };
      getWxExpressInfos(params).then((res) => {
        console.log(res);
        if (res.data.count > 0) {
          this.form = res.data.results[0];
        }
      });
    },
    onSubmit() {
      if (!this.form.id) {
        const openid = localStorage.getItem("openid");
        this.form["openid"] = openid;
        this.addExpressInfo();    
      } else {
        this.updateExpressInfo();
      }
    },

    addExpressInfo() {
      addWxExpressInfo(this.form)
        .then((res) => {
          console.log(res);
          this.form = res.data;
          this.$toast("提交成功!");
        })
        .catch((error) => {
          console.log(error);
          this.$toast("提交失败!");
        });
    },

    updateExpressInfo() {
      updateWxExpressInfo(this.form)
        .then((res) => {
          console.log(res.data);
          this.form = res.data;
          this.$toast("提交成功!");
        })
        .catch((error) => {
          console.log(error);
          this.$toast("提交失败!");
        });
    },
  },
};
</script>

<style scoped>
.van-cell--large {
  font-weight: bold;
}
</style>
